package com.wwj.admin.service.impl;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wwj.admin.Vo.CodeVo;
import com.wwj.admin.Vo.UserVo;
import com.wwj.admin.constant.ConstantFinal;
import com.wwj.admin.dao.UserDao;
import com.wwj.admin.entity.UserEntity;
import com.wwj.admin.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserDao, UserEntity> implements UserService {

    @Resource
    private RedisTemplate<String, String> redisTemplate;

    @Resource
    private UserDao userDao;

    /**
     * 第一次生成验证码一系列操作
     */
    @Override
    public CodeVo createCode() {
        // 生成验证码
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(120, 50);
        // 获取验证码
        String code = lineCaptcha.getCode();
        // 编码
        String codeBase64 = lineCaptcha.getImageBase64();
        String codeUUID = UUID.randomUUID().toString();
        // 存储Redis
        redisTemplate.opsForValue().set(ConstantFinal.REDIS_IDENTIFY_CODE_NAME + codeUUID, code, 2, TimeUnit.MINUTES);
        return new CodeVo(codeUUID, ConstantFinal.IMG_BASE64_PREFIX + codeBase64);
    }

    /**
     * 多次获取验证码操作
     */
    @Override
    public CodeVo againCreateCode(String codeUUID) {
        log.info("againCreateCode ===>  " + codeUUID);
        // Redis查询数据
        Boolean hasKey = redisTemplate.hasKey(ConstantFinal.REDIS_IDENTIFY_CODE_NAME + codeUUID);
        // 存在数据，移除数据
        if (Boolean.TRUE.equals(hasKey)) {
            redisTemplate.delete(ConstantFinal.REDIS_IDENTIFY_CODE_NAME + codeUUID);
        }
        return this.createCode();
    }

    /**
     * 校验验证码
     */
    @Override
    public boolean checkCode(UserVo userVo) {
        if (userVo.getCodeUUID() != null) {
            String codeForRedis = redisTemplate.opsForValue().get(ConstantFinal.REDIS_IDENTIFY_CODE_NAME + userVo.getCodeUUID());
            if (codeForRedis != null){
                redisTemplate.delete(ConstantFinal.REDIS_IDENTIFY_CODE_NAME + userVo.getCodeUUID());
                return codeForRedis.equalsIgnoreCase(userVo.getIdentifyCode());
            }
        }
        return false;
    }

    /**
     * 获取用户所有数据
     */
    @Override
    public PageInfo<UserEntity> getUserData(String currentPage, String pageSize, String query) {
        Page<UserEntity> page = PageHelper.startPage(Integer.parseInt(currentPage), Integer.parseInt(pageSize));
        QueryWrapper<UserEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne(ConstantFinal.FIELD_USER_IDENTITY,ConstantFinal.VALUE_ADMIN);
        if (!query.isEmpty()) {
            queryWrapper.and(wrapper ->{
                wrapper.like(ConstantFinal.FIELD_USER_ID, query).or()
                        .like(ConstantFinal.FIELD_USER_NAME, query).or()
                        .like(ConstantFinal.FIELD_MOBILE, query).or()
                        .like(ConstantFinal.FIELD_IDENTITY_NUMBER, query);
            });
        }
        queryWrapper.orderByAsc(ConstantFinal.FIELD_USER_ID);
        userDao.selectList(queryWrapper);
        return page.toPageInfo();
    }
}
